﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace VTLive.Models
{
    public class EpisodeRepository
    {
        VTliveDataContext db = new VTliveDataContext();

        public IQueryable<Episode> GetAllEpisodes()
        {
            return db.Episodes;
        }

        public IQueryable<Episode> GetAllEpisodesOfSchedule(int id)
        {
            return from episode in db.Episodes where episode.ScheduleID == id select episode;
        }

        public Episode GetEpisode(int EpisodeID)
        {
            return db.Episodes.SingleOrDefault(s => s.ID == EpisodeID);
        }

        public void Add(Episode MyEpisode)
        {
            db.Episodes.InsertOnSubmit(MyEpisode);
        }

        public void Delete(Episode MyEpisode)
        {
            db.Episodes.DeleteOnSubmit(MyEpisode);
        }

        public void Save()
        {
            db.SubmitChanges();
        }

        internal int GetNextOrderNum(int p)
        // Skilar næsta raðnúmeri fyrir dagskrárliði en kastar villu ef dagskráin er tóm
        {
            return ((from episodes in db.Episodes where episodes.ScheduleID == p select episodes.OrderNum).Max() + 1);
        }

        internal Episode GetEpisodeByVideoID(int p)
        {
            return (from ep in db.Episodes where ep.VideoID == p select ep).SingleOrDefault();
        }
    }
}
